package com.dai.zhenfujin.my.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.dai.zhenfujin.BaseAdapter.BaseViewHolder;
import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.adapter.IssueTypeAdpter;
import com.dai.zhenfujin.adapter.IssueTypeAdpter2;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.TypeListBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.issue.activity.AlumIssueActivity;
import com.dai.zhenfujin.issue.activity.AlumIssueActivity2;
import com.dai.zhenfujin.utils.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AlumTypeActivity extends BaseActivity {
    private View view;
    private ImageView imageView, ivReturn;
    private TextView tvTitleName;
    private FrameLayout fl_img;
    private TextView tvRoom, tvVehicle, tvMarriage, tvOther;//tvFans;
    private RecyclerView rvLeft, rvRight, rvRight2;
    private List<TypeListBean.TypelistBean> typeList = new ArrayList<>();
    private List<TypeListBean.TypelistBean> typeList2 = new ArrayList<>();
    private List<TypeListBean.TypelistBean> typeList3 = new ArrayList<>();
    private IssueTypeAdpter issueTypeAdpter;
    private IssueTypeAdpter2 issueTypeAdpter2;
    private IssueTypeAdpter2 issueTypeAdpter3;
    private int type2;
    private String name;
    private String name2;
    private String t_name;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_issue2);
        rvLeft = findViewById(R.id.rv_left);
        rvRight = findViewById(R.id.rv_right);
        rvRight2 = findViewById(R.id.rv_right2);
        fl_img = findViewById(R.id.fl_img);
        tvTitleName = findViewById(R.id.title_textview);
        initData();
    }

    private void initData() {
        type2 = getIntent().getIntExtra("type", 0); //18908625109
        if (type2 == 1) {
            tvTitleName.setText("主业类别");
        } else if (type2 == 2) {
            tvTitleName.setText("副业类别");
        } else {
            tvTitleName.setText("发布类别");
        }
        rvLeft.setLayoutManager(new LinearLayoutManager(this));
        //设置Item增加、移除动画
        rvLeft.setItemAnimator(new DefaultItemAnimator());
        issueTypeAdpter = new IssueTypeAdpter(this, typeList, new TypeItemListener());
        rvLeft.setAdapter(issueTypeAdpter);

        rvRight.setLayoutManager(new LinearLayoutManager(this));
        //设置Item增加、移除动画
        rvRight.setItemAnimator(new DefaultItemAnimator());
        issueTypeAdpter2 = new IssueTypeAdpter2(this, typeList2, new TypeItemListener2());
        rvRight.setAdapter(issueTypeAdpter2);

        rvRight2.setLayoutManager(new LinearLayoutManager(this));
        //设置Item增加、移除动画
        rvRight2.setItemAnimator(new DefaultItemAnimator());
        issueTypeAdpter3 = new IssueTypeAdpter2(this, typeList3, new TypeItemListener3());
        rvRight2.setAdapter(issueTypeAdpter3);
        fl_img.setOnClickListener(v -> finish());
        if (type2 == 2) {
            //副业
            getIssueSidelinType();
        } else {
            //主业
            getIssueType();
        }
    }

    /**
     * 副业
     */
    private void getIssueSidelinType() {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("user_id", SPUtils.getString(this, Constants.USER_MOBILE));
        params.put("req_bd", params2);
        HttpClient.post(this, Constants.SIDELINE, params, new CallBack<TypeListBean>() {
            @Override
            public void onSuccess(TypeListBean resultBean) {
                typeList.clear();
                typeList.addAll(resultBean.list);
                issueTypeAdpter.notifyDataSetChanged();
            }
        });
    }

    //主业
    private void getIssueType() {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("type_id", 0 + "");
        params.put("req_bd", params2);
        HttpClient.post(this, Constants.TYPELIST, params, new CallBack<TypeListBean>() {
            @Override
            public void onSuccess(TypeListBean resultBean) {
                typeList.clear();
                typeList.addAll(resultBean.getTypeList());
                issueTypeAdpter.notifyDataSetChanged();
            }
        });
    }

    class TypeItemListener implements BaseViewHolder.onItemCommonClickListener {
        @Override
        public void onItemClickListener(int position) {
            if (type2 == 2) {   //副业
                typeList2.clear();
                typeList2.addAll(typeList.get(position).child_list);
                issueTypeAdpter2.notifyDataSetChanged();
                t_name = typeList.get(position).getT_name();
            } else {  //主业
                name = typeList.get(position).getT_name();
                if (name.equals("其他")){
                    Intent intent = new Intent();
                    intent.putExtra("name", name);
                    intent.putExtra("type_id", typeList.get(position).getT_id());
                    setResult(13, intent);
                    finish();
                    return;
                }

                String id = typeList.get(position).getT_id();
                Map<String, Object> params = new HashMap<>();
                Map<String, String> params2 = new HashMap<>();
                params2.put("type_id", id);
                params.put("req_bd", params2);
                HttpClient.post(AlumTypeActivity.this, Constants.TYPELIST, params, new CallBack<TypeListBean>() {
                    @Override
                    public void onSuccess(TypeListBean resultBean) {
                        typeList2.clear();
                        typeList2.addAll(resultBean.getTypeList());
                        issueTypeAdpter2.notifyDataSetChanged();
                        typeList3.clear();
                        issueTypeAdpter3.notifyDataSetChanged();
                    }
                });
            }
        }

        @Override
        public void onItemLongClickListener(int position) {

        }
    }

    class TypeItemListener2 implements BaseViewHolder.onItemCommonClickListener {
        @Override
        public void onItemClickListener(int position) {
            Intent intent = new Intent();
            //发布
            if (type2 == 2) {   //副业 招亲
              String fu_name = typeList2.get(position).getT_name();
                if (fu_name.equals("男") || fu_name.equals("女")) {
                    intent.putExtra("name", t_name + "—" + fu_name);
                    intent.putExtra("id", typeList2.get(position).getT_id());
                    intent.putExtra("type", 1);
                    intent.setClass(AlumTypeActivity.this, AlumIssueActivity2.class);
                    startActivity(intent);
                } else { //副业
                    intent.putExtra("name", t_name + "—" + fu_name);
                    intent.putExtra("id", typeList2.get(position).getT_id());
                    intent.putExtra("type", 2);
                    intent.setClass(AlumTypeActivity.this, AlumIssueActivity2.class);
                    startActivity(intent);
                }
            } else {
                //主业
                String id = typeList2.get(position).getT_id();
                name2 = typeList2.get(position).getT_name();
                Map<String, Object> params = new HashMap<>();
                Map<String, String> params2 = new HashMap<>();
                params2.put("type_id", id);
                params.put("req_bd", params2);
                HttpClient.post(AlumTypeActivity.this, Constants.TYPELIST, params, new CallBack<TypeListBean>() {
                    @Override
                    public void onSuccess(TypeListBean resultBean) {
                        if (resultBean.getTypeList().size() > 0) {
                            typeList3.clear();
                            typeList3.addAll(resultBean.getTypeList());
                            issueTypeAdpter3.notifyDataSetChanged();
                            if (type2 == 1) {  // 主业
                                //  String FristName = name + "-" + name2 + "-" + typeList3.get(position).getT_name();
                                //  intent.putExtra("name", FristName);
                                //  intent.putExtra("type_id", typeList2.get(position).getT_id());
                                //   setResult(13, intent);
                            } else {
                                 intent.putExtra("id", typeList3.get(position).getT_id());
                                  intent.putExtra("name", typeList3.get(position).getT_name());
                                  intent.setClass(AlumTypeActivity.this, AlumIssueActivity.class);
                                   startActivity(intent);
                            }
                        } else {
                            String FristName = name + "-" + name2;
                            intent.putExtra("name", FristName);
                            intent.putExtra("type_id", typeList2.get(position).getT_id());
                            setResult(13, intent);
                            finish();
                        }
                    }
                });
            }
        }


        @Override
        public void onItemLongClickListener(int position) {

        }
    }


    class TypeItemListener3 implements BaseViewHolder.onItemCommonClickListener {
        @Override
        public void onItemClickListener(int position) {
            String FristName = name + "-" + name2 + "-" + typeList3.get(position).getT_name();
            Intent intent = new Intent();
            intent.putExtra("name", FristName);
            intent.putExtra("type_id", typeList3.get(position).getT_id());
            setResult(13, intent);
            finish();
        }

        @Override
        public void onItemLongClickListener(int position) {

        }
    }

}
